package com.skit.score.fragment

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.skit.common.base.BaseFragment
import com.skit.score.R
import kotlinx.android.synthetic.main.score_main_fragment.*

/**
 * @author shuike [Contact me.](mailto:shuike007@gmail.com)
 * @version 1.0
 * @since 2020/5/28
 */

class ScoreFragment : BaseFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.score_main_fragment, container, false)
    }

    companion object {
        fun newInstance(): ScoreFragment {
            val args = Bundle()
            val fragment = ScoreFragment()
            fragment.arguments = args
            return fragment
        }
    }

    //中间显示的文字数据
    private fun generateCenterSpannableText(): SpannableString? {
        val s = SpannableString("信用分数\n769")
        s.setSpan(RelativeSizeSpan(1.2f), 0, 4, 0)
        s.setSpan(StyleSpan(Typeface.NORMAL), 0, 4, 0)
        s.setSpan(ForegroundColorSpan(Color.BLACK), 0, 4, 0)
        s.setSpan(RelativeSizeSpan(3.8f), 4, s.length, 0)
        s.setSpan(StyleSpan(Typeface.BOLD), 4, s.length, 0)
        s.setSpan(ForegroundColorSpan(Color.RED), 4, s.length, 0)
        return s
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        val title = view.findViewById<TextView>(R.id.top_title)
//        title.text = "成绩"
        pieChart.setNoDataText("无数据")
        pieChart.also { pieChart ->
            pieChart.setTouchEnabled(true);
            //设置饼图是否使用百分比
            pieChart.setUsePercentValues(true);
            pieChart.holeRadius = 100f
            pieChart.isDrawHoleEnabled = true
            //设置饼图右下角的文字描述
//            pieChart.setDescription("");
            //设置饼图右下角的文字描述   文字位置
//            pieChart.setDescriptionPosition(460,730);
            //设置饼图右下角的文字大小
//            pieChart.setDescriptionTextSize(16);
//            pieChart.setExtraOffsets(15f, -20f, 15f, 0f);//left   top  right  bottom

            //是否显示圆盘中间文字，默认显示
            pieChart.setDrawCenterText(true);
            pieChart.setHoleColor(Color.WHITE);//设置中间圆盘的颜色
            //设置圆盘中间文字
            pieChart.setCenterText(generateCenterSpannableText());
//            pieChart.setTransparentCircleColor(getResources().getColor(R.color.bantouming));
//        pieChart.setTransparentCircleAlpha(100);

            pieChart.setHoleRadius(60f); //设置中间圆盘的半径,值为所占饼图的百分比
            pieChart.setTransparentCircleRadius(63f); //设置中间透明圈的半径,值为所占饼图的百分比

            pieChart.setDrawCenterText(true);//饼状图中间可以添加文字

            //设置圆盘是否转动，默认转动
            pieChart.setRotationEnabled(false);
            //设置初始旋转角度
            pieChart.setRotationAngle(100f);
        }

        val pieEntry1 = PieEntry(10f, "科技活动")
        val pieEntry2 = PieEntry(40f, "志愿者服务活动")

        val list = mutableListOf(pieEntry1, pieEntry2)

        val pieDataSet = PieDataSet(list, "")

        //一般有多少项数据，就配置多少个颜色的，少的话会复用最后一个颜色，多的话无大碍
        pieDataSet.colors = mutableListOf(
            ContextCompat.getColor(context!!, R.color.blue),
            ContextCompat.getColor(context!!, R.color.green)
        )

        val pieData = PieData(pieDataSet)

        pieChart.data = pieData
        //显示值格式化，这里使用Api,添加百分号
        pieData.setValueFormatter(PercentFormatter())
        //设置值得颜色
        pieData.setValueTextColor(Color.parseColor("#FFFFFF"))
        //设置值得大小
        pieData.setValueTextSize(10f)

        val description = Description()

        description.text = ""
        //把右下边的Description label 去掉，同学也可以设置成饼图说明
        pieChart.description = description

        //去掉中心圆，此时中心圆半透明
        pieChart.holeRadius = 0f
        //去掉半透明
        pieChart.setTransparentCircleAlpha(0)

        pieChart.setDrawEntryLabels(true)

        pieChart.invalidate()
    }
}